#include "admin_interface.h"
#include "ui_admin_interface.h"
#include "test.h"
#include "ui_test.h"
#include "utils.h"
#include "studentstore.h"
#include "bookinfo.h"
#include "bookstore.h"
#include <vector>
#include <iostream>
#include <QMessageBox>

class signin;
using namespace utils;
using namespace student;
using namespace studentstore;
using namespace bookandrecord;
using namespace bookstore;
void admin_interface::F5() {
    page_stu = 1;
    confstu_num = 1;
    confmenunum = 1;
    page_menu = 1;
    confmenu_num = 1;

}
void admin_interface::clearstu_buttom() {
    ui->pushButton_15->setEnabled(false);
    ui->pushButton_15->hide();
    ui->pushButton_16->setEnabled(false);
    ui->pushButton_16->hide();
    ui->pushButton_25->setEnabled(false);
    ui->pushButton_25->hide();
    ui->pushButton_26->setEnabled(false);
    ui->pushButton_26->hide();
    ui->pushButton_35->setEnabled(false);
    ui->pushButton_35->hide();
    ui->pushButton_36->setEnabled(false);
    ui->pushButton_36->hide();
}
void admin_interface::showstu_buttom1() {
    ui->pushButton_15->setEnabled(true);
    ui->pushButton_15->show();
    ui->pushButton_16->setEnabled(true);
    ui->pushButton_16->show();
}
void admin_interface::showstu_buttom2() {
    ui->pushButton_25->setEnabled(true);
    ui->pushButton_25->show();
    ui->pushButton_26->setEnabled(true);
    ui->pushButton_26->show();
}
void admin_interface::showstu_buttom3() {
    ui->pushButton_35->setEnabled(true);
    ui->pushButton_35->show();
    ui->pushButton_36->setEnabled(true);
    ui->pushButton_36->show();
}
void admin_interface::clearstu_line1() {
    ui->label_11->clear();
    ui->label_12->clear();
    ui->label_13->clear();
    ui->label_14->clear();
}
void admin_interface::clearstu_line2() {
    ui->label_21->clear();
    ui->label_22->clear();
    ui->label_23->clear();
    ui->label_24->clear();
}
void admin_interface::clearstu_line3() {
    ui->label_31->clear();
    ui->label_32->clear();
    ui->label_33->clear();
    ui->label_34->clear();
}

void admin_interface::clearmenu_line1() {
    ui->label_41->clear();
    ui->label_42->clear();
    ui->label_43->clear();
    ui->label_44->clear();
}
void admin_interface::clearmenu_line2() {
    ui->label_51->clear();
    ui->label_52->clear();
    ui->label_53->clear();
    ui->label_54->clear();
}
void admin_interface::clearmenu_line3() {
    ui->label_61->clear();
    ui->label_62->clear();
    ui->label_63->clear();
    ui->label_64->clear();
}
void admin_interface::clearmenu_buttom() {
    ui->pushButton_45->setEnabled(false);
    ui->pushButton_45->hide();
    ui->pushButton_46->setEnabled(false);
    ui->pushButton_46->hide();
    ui->pushButton_55->setEnabled(false);
    ui->pushButton_55->hide();
    ui->pushButton_56->setEnabled(false);
    ui->pushButton_56->hide();
    ui->pushButton_65->setEnabled(false);
    ui->pushButton_65->hide();
    ui->pushButton_66->setEnabled(false);
    ui->pushButton_66->hide();
}
void admin_interface::showmenu_buttom1() {
    ui->pushButton_45->setEnabled(true);
    ui->pushButton_45->show();
    ui->pushButton_46->setEnabled(true);
    ui->pushButton_46->show();
}
void admin_interface::showmenu_buttom2() {
    ui->pushButton_55->setEnabled(true);
    ui->pushButton_55->show();
    ui->pushButton_56->setEnabled(true);
    ui->pushButton_56->show();
}
void admin_interface::showmenu_buttom3() {
    ui->pushButton_65->setEnabled(true);
    ui->pushButton_65->show();
    ui->pushButton_66->setEnabled(true);
    ui->pushButton_66->show();
}
admin_interface::admin_interface(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::admin_interface) {
    ui->setupUi(this);
    page_stu = 1;
    pagesize = 3;
    pagestu_num = 1;
    confstu_num = 1;
    page_menu = 1;
    pagemenu_num = 1;
    confmenu_num = 1;
    confmenunum = 1;
    reviseo = new revise(this);
    reviseo->hide();
    addadmin = new add_admin(this);
    addadmin->hide();
    bookinfopage = new bookinfo(this);
    bookinfopage->hide();
    addmenu = new debug_addMenu(this);
    addmenu->hide();
    showall = new deBug(this);
    showall->hide();
    exporto = new exportfile(this);
    exporto->hide();
    importUI = new importStudent();
    importUI->hide();
    hero = new herobank();
    hero->hide();
    change = new changeCode ();
    change->hide();
    addMessageUI = new add_Message(this);
    addMessageUI->hide();
    messageListUI = new show_messageList(this);
    messageListUI->hide();
    connect(this, SIGNAL(sendData(Student*)), reviseo, SLOT(getData(Student*)));
    connect(this, SIGNAL(sendData(Menu*)), bookinfopage, SLOT(getData(Menu*)));
    connect(this, SIGNAL(sendData(Student*)), change, SLOT(getData(Student*)));
    connect(this, SIGNAL(sendData(Student*,int)), addMessageUI, SLOT(getData(Student*,int)));
    connect(this, SIGNAL(sendData(Student*)), messageListUI, SLOT(getData(Student*)));
    ui->label_01->clear();
    ui->label_02->clear();
    ui->label_03->clear();
    ui->label_04->clear();

    clearstu_line1();
    clearstu_line2();
    clearstu_line3();
    clearstu_buttom();
    clearmenu_line1();
    clearmenu_line2();
    clearmenu_line3();
    clearmenu_buttom();
}
//前端发送信号，根据其判断是否隐藏添加管理员按键
void admin_interface::getData(Student *stu, int type) {
    thisadmin = stu;
    if(type == 0) { //普通管理员隐藏按键
        ui->pushButton->setEnabled(false);
        ui->pushButton->hide();
        QString admin_id = str2Qstr(stu->stu_id);
        ui->label_4->setText(admin_id);
        ui->pushButton_19->setEnabled(true);
        ui->pushButton_19->show();
        thisadmin = stu;
        this->type=0;
    } else if(type==2){//超级管理员
        ui->pushButton->setEnabled(true);
        ui->pushButton->show();
        ui->label_4->setText("888888");
        ui->pushButton_19->setEnabled(false);
        ui->pushButton_19->hide();
        thisadmin->ID=-1;
        strcpy(thisadmin->name,"超级管理员");
        strcpy(thisadmin->stu_id,"888888");
    }
    else{
        this->type=1;
        thisadmin=stu;
    }

}
admin_interface::~admin_interface() {
    delete ui;
}

//查询（按照名字查询用户）
void admin_interface::on_pushButton_3_clicked() {
    vector<Student*>().swap(stuList);
    QString str = ui->lineEdit->text(); //Qstring与int转化
    string searchKey = string((const char *)str.toLocal8Bit());
    if (str == NULL)
        QMessageBox::warning(this, "warning", "请输入");
    else {
        stuList = search_multipleReturn("useFuzzySearch", "name", searchKey, studentDatPath) ;
        if(stuList.size() == 0) {
            QMessageBox::warning(this, "warning", "查找不到此用户，请查看输入是否正确");
            clearstu_line1();
            clearstu_line2();
            clearstu_line3();
            clearstu_buttom();
            ui->label_02->clear();
            ui->label_01->clear();
            ui->lineEdit_2->clear();

            return ;
        }

        else {
            confstu_num = 1;
            pagestu_num = (stuList.size() + pagesize - 1) / pagesize;
            this->showstu(stuList, 1);
        }
    }
}
//按学号搜索
void admin_interface::on_pushButton_6_clicked() {
    vector<Student*>().swap(stuList);
    QString str = ui->lineEdit->text(); //Qstring与int转化
    string searchKey = string((const char *)str.toLocal8Bit());
    if (str == NULL)
        QMessageBox::warning(this, "warning", "请输入");
    else {
        vector<Student*>studentList = search_multipleReturn("useFuzzySearch", "stu_id", searchKey, studentDatPath) ;

        //Student* stu = search_oneReturn<Student>("stu_id", searchKey, studentDatPath) ;
        for(int i = 0; i < studentList.size(); i++) {
            if(studentList[i]->exist == 0) {
                studentList.erase(studentList.begin() + i);
            }
        }
        stuList = studentList;
        if(stuList.size() != 0) {
            //stuList.push_back(stu);
            confstu_num = 1;
            pagestu_num = (stuList.size() + pagesize - 1) / pagesize;
            this->showstu(stuList, 1);
        } else {
            QMessageBox::warning(this, "warning", "查找不到此用户，请查看输入是否正确");
            clearstu_line1();
            clearstu_line2();
            clearstu_line3();
            clearstu_buttom();
            ui->label_02->clear();
            ui->label_01->clear();
            ui->lineEdit_2->clear();
            return ;
        }
    }
}
//按学生主键搜索
void admin_interface::on_pushButton_7_clicked() {
    vector<Student*>().swap(stuList);
    QString str = ui->lineEdit->text(); //Qstring与int转化
    string searchKey = string((const char *)str.toLocal8Bit());
    FILE *file = openfile(studentDatPath, sizeof(Student)); //打开文件
    fseek(file, 0, SEEK_END);//fseek将文件指针指到文件末尾
    auto offset = ftell(file);//ftell得到文件从指针到文件头的大小（offset是文件总大小）
    int allstu = offset / sizeof(Student);
    if(isDigitStr(str)) {
        int tmp = atoi(Qstr2str(str).c_str());
        if(tmp >= allstu) {
            QMessageBox::warning(this, "warning", "超过学生数");
        } else {
            Student* stu = search<Student>(tmp, studentDatPath);
            if(stu->exist == 0)
                stu = NULL;
            if(stu != NULL) {
                stuList.push_back(stu);
                confstu_num = 1;
                pagestu_num = (stuList.size() + pagesize - 1) / pagesize;
                this->showstu(stuList, 1);
            } else {
                QMessageBox::warning(this, "warning", "查找不到此用户，请查看输入是否正确");
                clearstu_line1();
                clearstu_line2();
                clearstu_line3();
                clearstu_buttom();
                ui->label_02->clear();
                ui->label_01->clear();
                ui->lineEdit_2->clear();
                return ;
            }
        }
    } else {
        QMessageBox::warning(this, "warining", "请输入正确数字");
    }
}
//展示多条书籍目录信息（参数：书目list，页码）
void admin_interface::showmenu(vector<Menu*>menuList, int confnum) {

    int listnum = menuList.size();
    if (listnum == 0) {
        clearmenu_line1();
        clearmenu_buttom();
        clearmenu_line2();
        clearmenu_line3();
        return;
    }
    int i = (confnum - 1) * pagesize;
    QString tempCell ;
    tempCell = QString(QString::number(menuList[i]->ID));
    ui->label_41->setText(tempCell);
    tempCell = QString(QString::fromLocal8Bit(menuList[i]->name));
    ui->label_42->setText(tempCell);
    tempCell = QString(QString::fromLocal8Bit(menuList[i]->ISBN));
    ui->label_43->setText(tempCell);
    tempCell = QString(QString::fromLocal8Bit(menuList[i]->writer));
    ui->label_44->setText(tempCell);
    showmenu_buttom1();
    if(i + 1 >= menuList.size()) {
        clearmenu_line2();
        if(confnum == pagemenu_num) {
            ui->pushButton_55->setEnabled(false);
            ui->pushButton_55->hide();
            ui->pushButton_56->setEnabled(false);
            ui->pushButton_56->hide();
        }

    } else {
        tempCell = QString(QString::number(menuList[i + 1]->ID));
        ui->label_51->setText(tempCell);
        tempCell = QString(QString::fromLocal8Bit(menuList[i + 1]->name));
        ui->label_52->setText(tempCell);
        tempCell = QString(QString::fromLocal8Bit(menuList[i + 1]->ISBN));
        ui->label_53->setText(tempCell);
        tempCell = QString(QString::fromLocal8Bit(menuList[i + 1]->writer));
        ui->label_54->setText(tempCell);
        showmenu_buttom2();
    }
    if(i + 2 >= menuList.size()) {
        clearmenu_line3();
        if(confnum == pagemenu_num) {
            ui->pushButton_65->setEnabled(false);
            ui->pushButton_65->hide();
            ui->pushButton_66->setEnabled(false);
            ui->pushButton_66->hide();
        }
    } else {
        tempCell = QString(QString::number(menuList[i + 2]->ID));
        ui->label_61->setText(tempCell);
        tempCell = QString(QString::fromLocal8Bit(menuList[i + 2]->name));
        ui->label_62->setText(tempCell);
        tempCell = QString(QString::fromLocal8Bit(menuList[i + 2]->ISBN));
        ui->label_63->setText(tempCell);
        tempCell = QString(QString::fromLocal8Bit(menuList[i + 2]->writer));
        ui->label_64->setText(tempCell);
        showmenu_buttom3();
    }

    tempCell = QString(QString::number(pagemenu_num));
    ui->label_04->setText(tempCell);
    tempCell = QString(QString::number(listnum));
    ui->label_03->setText(tempCell);
    tempCell = QString(QString::number(confnum));
    ui->lineEdit_5->setText(tempCell);
}

//显示多条学生信息（参数：学生list，页码）
void admin_interface::showstu(vector<Student*>stuList, int confnum = 1) {
    int listnum = stuList.size();
    if (listnum == 0) {
        QMessageBox::warning(this, "warning", "查找不到此用户，请查看输入是否正确");
        return;
    }
    int i = (confnum - 1) * pagesize;
    QString tempCell ;

    tempCell = QString(QString::number(stuList[i]->ID));
    ui->label_11->setText(tempCell);
    tempCell = QString(QString::fromLocal8Bit(stuList[i]->name));
    ui->label_12->setText(tempCell);
    tempCell = QString(QString::fromLocal8Bit(stuList[i]->stu_id));
    ui->label_13->setText(tempCell);
    tempCell = QString(QString::fromLocal8Bit(stuList[i]->major));
    ui->label_14->setText(tempCell);
    showstu_buttom1();
    if(i + 1 >= stuList.size()) {
        clearstu_line2();
        if(confnum == pagestu_num) {
            ui->pushButton_25->setEnabled(false);
            ui->pushButton_25->hide();
            ui->pushButton_26->setEnabled(false);
            ui->pushButton_26->hide();
        }

    } else {
        tempCell = QString(QString::number(stuList[i + 1]->ID));
        ui->label_21->setText(tempCell);
        tempCell = QString(QString::fromLocal8Bit(stuList[i + 1]->name));
        ui->label_22->setText(tempCell);
        tempCell = QString(QString::fromLocal8Bit(stuList[i + 1]->stu_id));
        ui->label_23->setText(tempCell);
        tempCell = QString(QString::fromLocal8Bit(stuList[i + 1]->major));
        ui->label_24->setText(tempCell);
        showstu_buttom2();
    }
    if(i + 2 >= stuList.size()) {
        clearstu_line3();
        if(confnum == pagestu_num) {
            ui->pushButton_35->setEnabled(false);
            ui->pushButton_35->hide();
            ui->pushButton_36->setEnabled(false);
            ui->pushButton_36->hide();
        }
    } else {
        tempCell = QString(QString::number(stuList[i + 2]->ID));
        ui->label_31->setText(tempCell);
        tempCell = QString(QString::fromLocal8Bit(stuList[i + 2]->name));
        ui->label_32->setText(tempCell);
        tempCell = QString(QString::fromLocal8Bit(stuList[i + 2]->stu_id));
        ui->label_33->setText(tempCell);
        tempCell = QString(QString::fromLocal8Bit(stuList[i + 2]->major));
        ui->label_34->setText(tempCell);
        showstu_buttom3();
    }

    tempCell = QString(QString::number(pagestu_num));
    ui->label_02->setText(tempCell);
    tempCell = QString(QString::number(listnum));
    ui->label_01->setText(tempCell);
    tempCell = QString(QString::number(confnum));
    ui->lineEdit_2->setText(tempCell);
}
//展示记录页
void admin_interface::showrecord() {

    int row = recordList.size(); //采取和增加用户时算ID大小一样的方法算数据行数
    ui->tableWidget->setColumnCount(5); //设置列数
    QStringList header;
    header << "ID" << "username" << "bookname" << "borrowTime"  << "returnTime" ;
    ui->tableWidget->setHorizontalHeaderLabels(header);
    ui->tableWidget->setRowCount(row);    //设置行数
    for (int i = 0; i < recordList.size(); i++) {      //展示信息
        ui->tableWidget->setRowCount(recordList.size());   //设置表格行数
        QString tempCell = QString(QString::number(recordList[i]->ID));
        ui->tableWidget->setItem(i, 0, new QTableWidgetItem(tempCell));//显示ID
        Student* stu = search<Student>(recordList[i]->userId, studentDatPath);
        tempCell = QString(QString::fromLocal8Bit(stu->name));
        ui->tableWidget->setItem(i, 1, new QTableWidgetItem(tempCell));//学生姓名
        Book* book = search<Book>(recordList[i]->bookId, bookDatPath);
        Menu* menu = search<Menu>(book->menu_ID, menuDatPath);
        tempCell = QString(QString::fromLocal8Bit(menu->name));//书籍名
        ui->tableWidget->setItem(i, 2, new QTableWidgetItem(tempCell));

        QString borrowTime = fomatTime(recordList[i]->borrowTime);
        QString returnTime;
        ui->tableWidget->setItem(i, 3, new QTableWidgetItem(borrowTime));
        if(recordList[i]->returnTime != NULL)
            returnTime = fomatTime(recordList[i]->returnTime);
        else
            returnTime = " ";
        ui->tableWidget->setItem(i, 4, new QTableWidgetItem(returnTime));

    }


}

//向上翻学生页
void admin_interface::on_pushButton_03_clicked() {
    confstu_num = page_stu - 1;
    page_stu--;
    if(page_stu < 1) {

        QMessageBox::warning(this, "warning", "已经是第一页");
        F5();
        return;
    } else
        this->showstu(stuList, confstu_num);
}
//向下翻学生页
void admin_interface::on_pushButton_04_clicked() {
    confstu_num = page_stu + 1;
    page_stu++;
    if(page_stu > pagestu_num) {
        QMessageBox::warning(this, "warning", "已经是最后一页");
        page_stu=pagestu_num;
        return;
    } else {
        this->showstu(stuList, confstu_num);
    }
}
//跳转学生页面
void admin_interface::on_pushButton_05_clicked() {
    QString str_ = ui->lineEdit_2->text();

    if(isDigitStr(str_)) {
        confstu_num = str_.toInt();
        if(confstu_num <= pagestu_num) {
            if(confstu_num >= 1 && confstu_num <= pagestu_num) {
                page_stu = confstu_num;
                this->showstu(stuList, confstu_num);
            }
        } else {
            F5();
            QMessageBox::warning(this, "warning", "超过页码");
        }
    } else {
        QMessageBox::warning(this, "warning", "输入错误");
        F5();
        ui->lineEdit_2->clear();
    }
}

//修改学生第一条信息
void admin_interface::on_pushButton_15_clicked() {
    Student* stu = stuList[(page_stu - 1) * pagesize];
    emit sendData(stu);
    reviseo->open();
    F5();
    clearstu_line1();
    clearstu_line2();
    clearstu_line3();
    clearstu_buttom();
}

void admin_interface::on_pushButton_25_clicked() {
    Student* stu = stuList[(page_stu - 1) * pagesize + 1];
    emit sendData(stu);
    reviseo->open();
    F5();
    clearstu_line1();
    clearstu_line2();
    clearstu_line3();
    clearstu_buttom();
}

void admin_interface::on_pushButton_35_clicked() {
    Student* stu = stuList[(page_stu - 1) * pagesize + 2];
    emit sendData(stu);
    reviseo->open();
    F5();
    clearstu_line1();
    clearstu_line2();
    clearstu_line3();
    clearstu_buttom();
}
//删除学生
void admin_interface::on_pushButton_16_clicked() {
    Student* stu = stuList[(page_stu - 1) * pagesize];
    int status = deleteStudent(stu->ID);
    if(status == 0) {
//        vector<Student*>().swap(stuList);

        QMessageBox::warning(this, "del", "确认删除");
        F5();
        clearstu_line1();
        clearstu_line2();
        clearstu_line3();
        clearstu_buttom();
        return;
    } else if(status == -1) {
        QMessageBox::warning(this, "del", "正在借阅无法删除");
        return;
    } else if(status == -2) {
        QMessageBox::warning(this, "del", "正在预约无法删除");
        return;
    }
}

void admin_interface::on_pushButton_26_clicked() {
    Student* stu = stuList[(page_stu - 1) * pagesize + 1];

    string str;
    //strcpy(str,"确认删除");
    str += stu->name;
    int status = deleteStudent(stu->ID);
    if(status == 0) {
//        vector<Student*>().swap(stuList);
        QMessageBox::warning(this, "del", "确认删除");
        F5();
        clearstu_line1();
        clearstu_line2();
        clearstu_line3();
        clearstu_buttom();
        return;
    } else if(status == -1) {
        QMessageBox::warning(this, "del", "正在借阅无法删除");
        return;
    } else if(status == -2) {
        QMessageBox::warning(this, "del", "正在预约无法删除");
        return;
    }
}

void admin_interface::on_pushButton_36_clicked() {
    Student* stu = stuList[(page_stu - 1) * pagesize + 2];
    int status = deleteStudent(stu->ID);
    if(status == 0) {
//        vector<Student*>().swap(stuList);
        QMessageBox::warning(this, "del", "确认删除");
        F5();
        clearstu_line1();
        clearstu_line2();
        clearstu_line3();
        clearstu_buttom();
        return;
    } else if(status == -1) {
        QMessageBox::warning(this, "del", "正在借阅无法删除");
        return;
    } else if(status == -2) {
        QMessageBox::warning(this, "del", "正在预约无法删除");
        return;
    }
}
//查找书籍目录（按照ID）
void admin_interface::on_pushButton_2_clicked() {
    vector<Menu*>().swap(menuList);
    QString str = ui->lineEdit_3->text(); //Qstring与int转化
    if(isDigitStr(str) == 0) {
        QMessageBox::warning(this, "warning", "请输入正确数字");
        return;
    } else {
        int searchKey = str.toInt();
        FILE *file = openfile(bookDatPath, sizeof(Book)); //打开文件
        fseek(file, 0, SEEK_END);//fseek将文件指针指到文件末尾
        auto offset = ftell(file);//ftell得到文件从指针到文件头的大小（offset是文件总大小）
        int allbooknum = offset / sizeof(Book); //确定ID大小
        if (str == NULL)
            QMessageBox::warning(this, "warning", "请输入");
        else if(searchKey >= allbooknum) {
            QMessageBox::warning(this, "warning", "超过书籍数大小");
        }

        else {
            menu = search<Menu>(searchKey, menuDatPath);
            Menu* temp = new Menu ();
            memset(temp, 0, sizeof (Menu));
            if(memcmp(menu, temp, sizeof (Menu)) == 0 || menu->exist != 1) {
                menuList.push_back(menu);
                QMessageBox::warning(this, "warning", "查找不到此书籍，请查看输入是否正确");
                clearmenu_line1();
                clearmenu_line2();
                clearmenu_line3();
                clearmenu_buttom();
                ui->label_03->clear();
                ui->label_04->clear();
                ui->lineEdit_5->clear();
                return ;
            } else {
                menuList.push_back(menu);
                confmenu_num = 1;
                pagemenu_num = (menuList.size() + pagesize - 1) / pagesize;
                this->showmenu(menuList, 1);
            }
            delete temp;
        }
    }
}
//搜索ISBN
void admin_interface::on_pushButton_9_clicked() {
    vector<Menu*>().swap(menuList);
    QString str = ui->lineEdit_3->text(); //Qstring与int转化
    string searchKey = Qstr2str(str);
    if (str == NULL)
        QMessageBox::warning(this, "warning", "请输入");
    else {
        vector<Menu*> menuL = search_multiplemenuReturn("useFuzzySearch", "ISBN", searchKey, menuDatPath);

        for(int i = 0; i < menuL.size(); i++) {
            if(menuL[i]->exist == 0) {
                menuL.erase(menuL.begin() + i);
            }
        }
        menuList = menuL;
        if(menuList.size() == 0) {
            QMessageBox::warning(this, "warning", "查找不到此书籍，请查看输入是否正确");
            clearmenu_line1();
            clearmenu_line2();
            clearmenu_line3();
            clearmenu_buttom();
            ui->label_03->clear();
            ui->label_04->clear();
            ui->lineEdit_5->clear();
            return ;
        }

        else {
            F5();
            pagemenu_num = (menuList.size() + pagesize - 1) / pagesize;
            this->showmenu(menuList, 1);
        }
    }
}
//搜索书名
void admin_interface::on_pushButton_8_clicked() {
    vector<Menu*>().swap(menuList);
    QString str = ui->lineEdit_3->text(); //Qstring与int转化
    string searchKey = Qstr2str(str);
    if (str == NULL)
        QMessageBox::warning(this, "warning", "请输入");
    else {
        menuList = search_multiplemenuReturn("useFuzzySearch", "name", searchKey, menuDatPath);
        if(menuList.size() == 0) {
            QMessageBox::warning(this, "warning", "查找不到此书籍，请查看输入是否正确");
            clearmenu_line1();
            clearmenu_line2();
            clearmenu_line3();
            clearmenu_buttom();
            ui->label_03->clear();
            ui->label_04->clear();
            ui->lineEdit_5->clear();
            return ;
        }

        else {
            F5();
            pagemenu_num = (menuList.size() + pagesize - 1) / pagesize;
            this->showmenu(menuList, 1);
        }
    }
}
//搜索作者
void admin_interface::on_pushButton_10_clicked() {
    vector<Menu*>().swap(menuList);
    QString str = ui->lineEdit_3->text(); //Qstring与int转化
    string searchKey = Qstr2str(str);
    if (str == NULL)
        QMessageBox::warning(this, "warning", "请输入");
    else {
        menuList = search_multiplemenuReturn("useFuzzySearch", "writer", searchKey, menuDatPath);
        if(menuList.size() == 0) {
            QMessageBox::warning(this, "warning", "查找不到此书籍，请查看输入是否正确");
            clearmenu_line1();
            clearmenu_line2();
            clearmenu_line3();
            clearmenu_buttom();
            ui->label_03->clear();
            ui->label_04->clear();
            ui->lineEdit_5->clear();
            return ;
        }

        else {
            F5();
            pagemenu_num = (menuList.size() + pagesize - 1) / pagesize;
            this->showmenu(menuList, 1);
        }
    }
}
//书籍上一页
void admin_interface::on_pushButton_06_clicked() {
    confmenunum = page_menu - 1;
    page_menu--;
    if(page_menu < 1) {
        QMessageBox::warning(this, "warning", "已经是第一页");
        F5();
        return;
    } else
        this->showmenu(menuList, confmenunum);
}
//书籍下一页
void admin_interface::on_pushButton_07_clicked() {
    confmenunum = page_menu + 1;
    page_menu++;
    if(page_menu > pagemenu_num) {
        QMessageBox::warning(this, "warning", "已经是最后一页");
        page_menu=pagemenu_num;
        return;
    } else
        this->showmenu(menuList, confmenunum);
}
//跳转页数
void admin_interface::on_pushButton_08_clicked() {
    QString str_ = ui->lineEdit_5->text();
    if(isDigitStr(str_)) {
        confmenunum = str_.toInt();
        if(confmenunum > pagemenu_num) {
            F5();
            QMessageBox::warning(this, "warning", "超过页数");
        } else {
            if(confmenunum >= 1 && confmenunum <= pagestu_num) {
                page_menu = confmenunum;
                this->showmenu(menuList, confmenunum);
            }
        }
    } else {
        QMessageBox::warning(this, "warning", "输入错误");
        ui->lineEdit_5->clear();
    }
}
//超管添加管理员
void admin_interface::on_pushButton_clicked() {
    addadmin->show();
}
//查看书籍详情
void admin_interface::on_pushButton_45_clicked() {
    Menu* menu = menuList[(page_menu - 1) * pagesize];
    emit sendData(menu);
    bookinfopage->show();
}

void admin_interface::on_pushButton_55_clicked() {
    Menu* menu = menuList[(page_menu - 1) * pagesize + 1];
    emit sendData(menu);
    bookinfopage->show();
}

void admin_interface::on_pushButton_65_clicked() {
    Menu* menu = menuList[(page_menu - 1) * pagesize + 2];
    emit sendData(menu);
    bookinfopage->show();
}



//根据学生ID查找记录
void admin_interface::on_pushButton_4_clicked() {
    vector<Record*>().swap(recordList);

    QString str = ui->lineEdit_4->text(); //Qstring与int转化
    if(isDigitStr(str) == 0) {
        QMessageBox::warning(this, "warning", "请输入正确数字");
        return;
    } else {
        int searchKey = str.toInt();
        if (str == NULL)
            QMessageBox::warning(this, "warning", "请输入");
        else {
            recordList = search_multiplerecordReturn("notFuzzySearch", "userID", searchKey, recordDatPath);
            FILE *file = openfile(studentDatPath, sizeof(Student)); //打开文件
            fseek(file, 0, SEEK_END);//fseek将文件指针指到文件末尾
            auto offset = ftell(file);//ftell得到文件从指针到文件头的大小（offset是文件总大小）
            int allstunum = offset / sizeof(Student); //确定ID大小
            if(searchKey >= allstunum) {
                QMessageBox::warning(this, "warning", "请正确输入");
                return;
            } else if(recordList.size() == 0) {
                QMessageBox::warning(this, "warning", "该用户无历史借阅记录");
                return ;
            }

            else {
                this->showrecord();
            }
        }
    }

}
//根据书籍ID查找
void admin_interface::on_pushButton_5_clicked() {
    vector<Record*>().swap(recordList);

    QString str = ui->lineEdit_4->text(); //Qstring与int转化
    if(isDigitStr(str) == 0) {
        QMessageBox::warning(this, "warning", "请正确输入");
    } else {

        int searchKey = str.toInt();

        FILE *file = openfile(bookDatPath, sizeof(Book)); //打开文件
        fseek(file, 0, SEEK_END);//fseek将文件指针指到文件末尾
        auto offset = ftell(file);//ftell得到文件从指针到文件头的大小（offset是文件总大小）
        int allbooknum = offset / sizeof(Book); //确定ID大小
        if (str == NULL)
            QMessageBox::warning(this, "warning", "请输入");
        else if(searchKey >= allbooknum) {
            QMessageBox::warning(this, "warning", "超过书籍数大小");
        } else {
            recordList = search_multiplerecordReturn("notFuzzySearch", "bookID", searchKey, recordDatPath);

            if(recordList.size() == 0) {
                QMessageBox::warning(this, "warning", "该书籍无历史被借阅记录");
                return ;
            }

            else {
                this->showrecord();
            }
        }
    }
}
//删除书目
void admin_interface::on_pushButton_46_clicked() {
    Menu* menu = menuList[(page_menu - 1) * pagesize];
    int status = deleteMenu(menu->ID);
    if(status == 0) {
        F5();
        QMessageBox::warning(this, "del", "删除成功");
        menuList.clear();
        ui->label_03->clear();
        vector<Menu*>().swap(menuList);
        ui->lineEdit_3->clear();
        this->showmenu(menuList, confmenunum);
    } else if(status == -1) {
        QMessageBox::warning(this, "del", "正在借阅无法删除");
        return;
    } else if(status == -2) {
        QMessageBox::warning(this, "del", "正在预约无法删除");
        return;
    }
}

void admin_interface::on_pushButton_56_clicked() {
    Menu* menu = menuList[(page_menu - 1) * pagesize + 1];
    int status = deleteMenu(menu->ID);
    if(status == 0) {
        F5();
        vector<Menu*>().swap(menuList);
        menuList.clear();
        ui->label_03->clear();
        QMessageBox::warning(this, "del", "删除成功");
        ui->lineEdit_3->clear();
        this->showmenu(menuList, confmenunum);
        return;
    } else if(status == -1) {
        QMessageBox::warning(this, "del", "正在借阅无法删除");
        return;
    } else if(status == -2) {
        QMessageBox::warning(this, "del", "正在预约无法删除");
        return;
    }

}

void admin_interface::on_pushButton_66_clicked() {
    Menu* menu = menuList[(page_menu - 1) * pagesize + 2];
    int status = deleteMenu(menu->ID);
    if(status == 0) {
        F5();
        vector<Menu*>().swap(menuList);
        menuList.clear();
        ui->label_03->clear();
        ui->lineEdit_3->clear();
        QMessageBox::warning(this, "del", "删除成功");
        this->showmenu(menuList, confmenunum);
    } else if(status == -1) {
        QMessageBox::warning(this, "del", "正在借阅无法删除");
        return;
    } else if(status == -2) {
        QMessageBox::warning(this, "del", "正在预约无法删除");
        return;
    }
}
//管理员添加书籍
void admin_interface::on_pushButton_14_clicked() {
    addmenu->show();
}
//查看所有信息
void admin_interface::on_pushButton_11_clicked() {
    showall->show();
}

void admin_interface::on_pushButton_12_clicked() {
    exporto->exportstulist = stuList;
    exporto->exportmenulist = menuList;
    exporto->exportrecordlist = recordList;
    exporto->show();
}

void admin_interface::on_pushButton_13_clicked() {
    importUI->show();
}
//显示英雄榜
void admin_interface::on_pushButton_17_clicked() {
    hero->open();
}

void admin_interface::on_pushButton_18_clicked() {

    FILE *fp;
    fp = fopen(studentDatPath, "w+");
    fp = fopen(bookDatPath, "w+");
    fp = fopen(menuDatPath, "w+");
    fp = fopen(recordDatPath, "w+");
    fp = fopen(heroRankDatPath, "w+");
    fp = fopen(messageDatPath, "w+");
    fp = fopen(logPath, "w+");
    fclose(fp);
    cout << "Delete all data successfully" << endl;
}
//修改密码
void admin_interface::on_pushButton_19_clicked() {
    emit sendData(thisadmin);
    change->open();
}
//信箱
void admin_interface::on_pushButton_20_clicked()
{
    emit sendData(thisadmin);
    messageListUI->open();
}

void admin_interface::on_pushButton_21_clicked()
{
    emit sendData(thisadmin,type);
    addMessageUI->open();
}
